// pages/pavilion/pavilion.js
const app = getApp();
import {
  BusinessModels
} from "../../utils/models/business/Business-models";
var businessModels = new BusinessModels;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarHeight: app.globalData.navbarHeight,
    titleName: '爱住酒店',
    navBgColor: "white",
    isLeft: true,

    distance: 0, //距离

    productList: [{
      id: 1,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '11.80'
    }, {
      id: 2,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '12.80'
    }, {
      id: 3,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '13.80'
    }, {
      id: 4,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '14.80'
    }, {
      id: 5,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '15.80'
    }, {
      id: 6,
      images: '/images/test/product2.png',
      title: '16.8元抢海盐日式の小圆饼1箱！日式风味，香脆可口，美味入口',
      yuanjia: '19.80',
      xianjia: '16.80'
    }],

    msgList: [{
      headimgurl: 'https://wx.qlogo.cn/mmopen/vi_32/4Cx1kYXtw9Rz4CibDsOUFt4T9l4VtQfr7iaK7nIGBgM7RDmdEqtqPxTZN4FrNpNLrnKNNcINaWR9CPsuIGNmeQBQ/132',
      msg: '.悄悄留下了足迹'
    }, {
      headimgurl: 'https://wx.qlogo.cn/mmopen/vi_32/4Cx1kYXtw9Rz4CibDsOUFt4T9l4VtQfr7iaK7nIGBgM7RDmdEqtqPxTZN4FrNpNLrnKNNcINaWR9CPsuIGNmeQBQ/132',
      msg: '冷少购买了2栋碧桂园'
    }, {
      headimgurl: 'https://wx.qlogo.cn/mmopen/vi_32/4Cx1kYXtw9Rz4CibDsOUFt4T9l4VtQfr7iaK7nIGBgM7RDmdEqtqPxTZN4FrNpNLrnKNNcINaWR9CPsuIGNmeQBQ/132',
      msg: '杰少购买了5栋碧桂园'
    }],

    isshow: false, //是否展开
    tabIndex: 0,
    tabList: [{
      id: 1,
      title: '优惠'
    }, {
      id: 2,
      title: '到店套餐'
    }],
    business:{},
    is_collect:0
  },

  // 展开收缩商品列表
  onProduct: function () {
    this.setData({
      isshow: !this.data.isshow
    });
  },

  // 跳转到产品详情
  onDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../product/product_detail?id=' + id,
    })
  },

  // 拨打手机号码
  phoneCall: function (e) {
    var tel = this.data.business.phone;
    if (tel) {
      wx.makePhoneCall({
        phoneNumber: tel,
        success: function (res) {

        },
        fail: function (res) {
          console.log('拨打失败')
        }
      })
    }

  },

  // 打开地图查看地址
  onLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success(res) {
        const latitude = parseFloat(that.data.business.latitude);
        const longitude = parseFloat(that.data.business.longitude);

        wx.openLocation({
          latitude,
          longitude,
          scale: 18,
          name: that.data.business.title,
          address: that.data.business.address,
        })
      }
    })
  },

  goMy: function () {
    wx.switchTab({
      url: '../my/my',
    })
  },

  // 切换菜单栏
  onTab: function (e) {
    this.setData({
      tabIndex: e.currentTarget.dataset.index
    });
  },
  collect:function(){
    var id = this.data.business.id;
    var that = this;
    var business = that.data.business;
    businessModels.collect(id,function(res){
      if(res.data.status==1){
        business.collect_num++;
      }else{
        business.collect_num--;
      }
      that.setData({
        is_collect:res.data.status,
        business: business
      })
    })
  },

  /**
   * @desc 由经纬度计算两点之间的距离，la为latitude缩写，lo为longitude
   * @param la1 第一个坐标点的纬度
   * @param lo1 第一个坐标点的经度
   * @param la2 第二个坐标点的纬度
   * @param lo2 第二个坐标点的经度
   * @return (int)s   返回距离(单位千米或公里)
   */
  distance: function (la1, lo1, la2, lo2) {
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137; //地球半径
    s = Math.round(s * 10000) / 10000;
    s = s.toFixed(2);
    return s;
  },
  getBusinessInfo:function(id){
    let that = this;
    businessModels.businessDetail(id,function(res){
      that.setData({
        titleName: res.data.business.title,
        business:res.data.business,
        productList: res.data.recommend_list,
        msgList: res.data.lunbo,
        is_collect: res.data.business.is_collect
      })
    })
  },
  share:function(){
    let that = this;
    let id = this.data.business.id;
    let business = this.data.business;
    businessModels.share(id, function (res) {
      business.share_num++;
      that.setData({
        business:business
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var business_id = options.id;
    // var business_id = 4;
    this.getBusinessInfo(business_id);
    var that = this;
    // 以北京故宫为例计算当前位置到其的距离，北京故宫坐标（116.403802, 39.915405）
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        console.log("当前坐标信息：", res)
        var distance = that.distance(res.latitude, res.longitude, 23.160862, 112.911141, 23.160862, 112.911141);
        console.log("当前位置距离店铺：", distance, "千米")

        that.setData({
          distance: distance
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})